package com.ecoolex.chinadutyfree.retailer.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderAfterSales;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderAfterSalesRecord;
import com.ecoolex.chinadutyfree.retailer.enums.DirectMailGoodsOrderAfterSalesStatus;
import com.ecoolex.chinadutyfree.retailer.ws.DirectMailGoodsOrderService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.User, description = "商品订单售后操作")
public class DirectMailGoodsOrderAfterSalesGraphql {
	
	@Reference
	private DirectMailGoodsOrderService directMailGoodsOrderService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = DirectMailGoodsOrderAfterSales.class,field = "canCancelApply",description = "能撤销售后申请")
	public boolean canCancelApply(@Source DirectMailGoodsOrderAfterSales source) {
		return DirectMailGoodsOrderAfterSalesStatus.canCancelApply(source.getStatus());
	}
	
	@TypeField(type = DirectMailGoodsOrderAfterSales.class,field = "canWriteDeliveryNo",description = "能填写物流单号")
	public boolean canWriteDeliveryNo(@Source DirectMailGoodsOrderAfterSales source) {
		return DirectMailGoodsOrderAfterSalesStatus.canWriteDeliveryNo(source.getStatus());
	}
	
	@TypeField(type = DirectMailGoodsOrderAfterSales.class,field = "directMailGoodsOrder",description = "售后单关联的订单")
    public DirectMailGoodsOrder directMailGoodsOrder(@Source DirectMailGoodsOrderAfterSales source) {
    	return directMailGoodsOrderService.getOrderById(source.getOrderId());
    }
	
	@TypeField(type = DirectMailGoodsOrder.class,field = "afterSales",description = "售后单")
	public DirectMailGoodsOrderAfterSales afterSales(@Source DirectMailGoodsOrder source) {
		return directMailGoodsOrderService.getDirectMailGoodsOrderAfterSalesByOrderId(source.getOrderId());
	}
	
	@TypeField(type = DirectMailGoodsOrderAfterSales.class,field = "afterSalesRecord",description = "售后记录列表")
	public List<DirectMailGoodsOrderAfterSalesRecord> afterSalesRecordList(@Source DirectMailGoodsOrderAfterSales source){
		return directMailGoodsOrderService.afterSalesRecordList(source.getAfterSalesId());
	}
	
	@TypeField(type = DirectMailGoodsOrderAfterSales.class,field = "afterSalesEvidenceImage",description = "售后单凭证图片")
	public List<StorageInfo> afterSalesEvidenceImage(@Source DirectMailGoodsOrderAfterSales source){
		String evidenceImage = source.getEvidenceImage();
		if(Check.notNull(evidenceImage)) {
			JSONArray jsonArray = JSONArray.parseArray(source.getEvidenceImage());
			List<String> list = jsonArray.toJavaList(String.class);
			return list.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
		}
		return null;
	}
	
	@QueryField(field = "directMailGoodsOrderAfterSalesList",description = "售后单列表")
	public Pagination<DirectMailGoodsOrderAfterSales> query(@NonNull Page page,ConsumerMemberAuthority user){
		user.assertLogin();
		return directMailGoodsOrderService.queryDirectMailGoodsOrderAfterSalesForUser(user.userId(), page);
	}
	
	@QueryField(field = "directMailGoodsOrderAfterSales",description = "售后单详情")
	public DirectMailGoodsOrderAfterSales get(@NonNull Integer afterSalesId,ConsumerMemberAuthority user) {
		user.assertLogin();
		return directMailGoodsOrderService.getDirectMailGoodsOrderAfterSalesByAfterSalesIdAndUserId(afterSalesId, user.userId());
	}
	
	@MutationField(field = "uploadAfterSalesEvidenceImage", description = { "上传售后凭证图片" })
    public StorageInfo uploadAfterSalesEvidenceImage(@NonNull Upload request, ConsumerMemberAuthority user) {
        user.assertLogin();

        UploadSource uploadSource = new UploadSource("DirectMailGoodsOrderAfterSales", "EvidenceImage");

        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(20));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
    }
	
	/**
	 * 用户申请退款，需要后台操作同意
	 * @param orderId
	 * @return
	 */
	@MutationField(field = "applyDirectMailGoodsOrderRefund",description = "申请订单退款")
	public DirectMailGoodsOrder applyRefund(@NonNull Integer orderId,String reason,String evidenceImage,ConsumerMemberAuthority user){
		user.assertLogin();
		DirectMailGoodsOrder order = directMailGoodsOrderService.getByOrderId(orderId);
		Assert.isTrue(Check.notNull(order),ResultCode.VERIFY.build(1), "商品订单不存在");
		return directMailGoodsOrderService.applyRefund(order,reason,evidenceImage,user.userName());
	}
	
	/**
	 * 用户退货申请
	 * @param orderId
	 * @param reason
	 * @param evidenceImage
	 * @param user
	 * @return
	 */
	@MutationField(field = "applyDirectMailGoodsOrderReturn",description = "申请订单退货")
	public DirectMailGoodsOrder applyReturn(@NonNull Integer orderId,String reason,String evidenceImage,ConsumerMemberAuthority user) {
		user.assertLogin();
		DirectMailGoodsOrder order = directMailGoodsOrderService.getByOrderId(orderId);
		Assert.isTrue(Check.notNull(order),ResultCode.VERIFY.build(1), "商品订单不存在");
		return directMailGoodsOrderService.applyReturn(order, reason, evidenceImage, user.userName());
	}
	
	/**
	 * 用户换货申请
	 * @param orderId
	 * @param reason
	 * @param evidenceImage
	 * @param user
	 * @return
	 */
	@MutationField(field = "applyDirectMailGoodsOrderExchange",description = "申请订单换货")
	public DirectMailGoodsOrder applyExchange(@NonNull Integer orderId,String reason,String evidenceImage,ConsumerMemberAuthority user) {
		user.assertLogin();
		DirectMailGoodsOrder order = directMailGoodsOrderService.getByOrderId(orderId);
		Assert.isTrue(Check.notNull(order),ResultCode.VERIFY.build(1), "商品订单不存在");
		return directMailGoodsOrderService.applyExchange(order, reason, evidenceImage, user.userName());
	}
	
	/**
	 * 填写物流单号
	 * @param afterSalesId
	 * @param deliveryNo
	 * @param user
	 * @return
	 */
	@MutationField(field = "writeDeliveryNo",description = "填写物流单号")
	public DirectMailGoodsOrderAfterSales writeDeliveryNo(@NonNull Integer afterSalesId,@NonNull String deliveryNo,ConsumerMemberAuthority user) {
		user.assertLogin();
		DirectMailGoodsOrderAfterSales afterSales = directMailGoodsOrderService.getDirectMailGoodsOrderAfterSalesByAfterSalesId(afterSalesId);
		Assert.isTrue(Check.notNull(afterSales),ResultCode.VERIFY.build(1), "商品订单售后单不存在");
		return directMailGoodsOrderService.writeDeliveryNo(afterSales, deliveryNo, user.userName());
	}
	
	@MutationField(field = "cancelDirectMailGoodsOrderAfterSales",description = "撤销售后申请")
	public DirectMailGoodsOrderAfterSales cancelAfterSales(@NonNull Integer afterSalesId,ConsumerMemberAuthority user) {
		user.assertLogin();
		DirectMailGoodsOrderAfterSales afterSales = directMailGoodsOrderService.getDirectMailGoodsOrderAfterSalesByAfterSalesId(afterSalesId);
		Assert.isTrue(Check.notNull(afterSales),ResultCode.VERIFY.build(1), "商品订单售后单不存在");
		return directMailGoodsOrderService.cancelAfterSales(afterSales, user.userName());
	}
}
